<?php

namespace app\admin\controller\xmwms\purchase;

use app\common\controller\Backend;
use app\service\PurchaseService;
use think\facade\Db;

/**
 * 需求汇总管理
 */
class Demand extends Backend
{
     /**
      * Demand模型对象
      * @var object
      * @phpstan-var \app\admin\model\xmwms\purchase\Demand
      */
     protected object $model;

     protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

     protected string|array $quickSearchField = ['id'];

     public function initialize(): void
     {
          parent::initialize();
          $this->model = new \app\admin\model\xmwms\purchase\Demand;
     }


     /**
      * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
      */


     //获取数据库的需求列表
     public function index(): void
     {

          //新增按title查找对应数据

          $params = $this->request->param();
          if (empty($params['platform'])) {
               $params['platform'] = 1;
          }
          $platformData = PurchaseService::platform($params['platform']);
          $name = $platformData['name'] ?? 'ckm';
          $db = $platformData['db'] ?? Db::connect('mysql1');
          $res = PurchaseService::platform_demand($params, $db, $name);
          $this->success(
               '',
               [
                    'list' => $res['list'],
                    'total' => $res['total'],
               ]
          );
     }

     //获取采购中的数据
     public function purchase(): void
     {
          $params = $this->request->param();
          $res = PurchaseService::platform_purchase($params);
          $this->success(
               '',
               [
                    'list' => $res,
                    'total' => count($res),
               ]
          );
     }

}
